package com.znsd.zl.sys.sms.controller;

import com.znsd.zl.sys.sms.utils.AliSmsUtils;

import com.znsd.zl.utils.R;
import com.znsd.zl.vo.req.CaptchaCheckReqVo;
import com.znsd.zl.vo.req.SendSmsReqVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName SmsController
 * @Description 短信发送控制器接口
 * @Author jaypdd
 * @Date 2020/8/18 21:59
 * @Version v1.0
 */
@RestController
@RequestMapping("/app/sms")
public class SmsController {

	@Resource
	public AliSmsUtils smsUtils;

	/**
	 * 短信验证码发送
	 * @param phone         接收短信验证码的手机号码
	 * @param templateCode  短信模板id，可选
	 * @return  发送成功 code:0 失败 500
	 */
	@GetMapping("/sendSmsCaptcha")
	public R sendSmsCaptcha(@RequestParam(value = "phone") String phone,
	                        @RequestParam(value = "templateCode", required = false) String templateCode) {
		return smsUtils.sendSmsCaptcha(phone, templateCode) ? R.ok("短信发送成功") : R.error("短信发送失败");
	}

	/**
	 * 发送短信
	 * @param reqVo  短信发送VO
	 * @return  发送成功 code:0 失败 500
	 */
	@PostMapping("/sendSms")
	public R sendSms(@RequestBody SendSmsReqVo reqVo) {
		boolean sendStatus = smsUtils.sendContentSms(reqVo.getPhone(),
				reqVo.getContent(), reqVo.getTemplateCode());
		return sendStatus ? R.ok("发送成功") : R.ok("发送失败，请稍后重试");
	}

	/**
	 * 校验短信验证码
	 * @param reqVo  短信验证码校验VO
	 * @return 校验通过返回 code:0 失败code:500
	 */
	@PostMapping("/check")
	public R checkCaptcha(@RequestBody CaptchaCheckReqVo reqVo) {
		System.out.println(reqVo);
		return smsUtils.isCaptcha(reqVo.getPhone(), reqVo.getCaptcha()) ? R.ok("验证成功") : R.error("验证失败");
	}

	/**
	 * 发送预约成功短信
	 * @param reqVo  短信发送VO
	 * @return  发送成功 code:0 失败 500
	 */
	@PostMapping("/sendReservedSms")
	public R sendReservedSms(@RequestBody SendSmsReqVo reqVo) {
		boolean sendStatus = smsUtils.sendReservedSms(reqVo.getPhone(),
				reqVo.getContent(), reqVo.getTemplateCode());
		return sendStatus ? R.ok("发送成功") : R.ok("发送失败，请稍后重试");
	}


}
